/**
 * Copyright 2016 Qiyi Inc. All rights reserved.
 *
 * @file:   StatisticOcr2.Util.js
 * @path:   js-src/StatisticOcr/
 * @desc:
 * @author: karkarrotxu@qiyi.com
 * @date:   2016-06-26
 */
qui(function (require, exports, module) {

    StatisticOcr2.Util = $.extend(true, {}, EpgWeb.Util, {
        formatter: {
            addTime: function (v, o, r) {
                return v.split(" ")[0];
            },
            percent: function (v, o, r) {
                return v + "%";
            },
            ocrFlag: function (v, o, r) {
                if (v == 1) {
                    return "是";
                }
                return "否";
            },
            operator_download: function (v, o, r) {
                var htmlArr = [];
                htmlArr.push("<a href='javascript:;' onclick='StatisticOcr2.Util.downloadData(" + r.id + ")'>下载</a> ")
                return htmlArr.join("");
            },
            titleCharLen:function( v, o, r ){
                return v/2;
            }
        },
        doQueryAvgPercent: function () {
            var paramsObj = $("#list").jqGrid("getGridParam", "postData").paramsObj;
            if (paramsObj != null) {
                StatisticOcr2.Dao.getAvgAndFullPercent({ paramsObj: paramsObj }, function (data) {
                    $("#query_avg_loading").hide();
                    if (data != null && data.code == "A00000") {
                        if (Number(data.data.avg) >= 0) {
                            $("span[name=span_avg_percent]").html(Number(data.data.avg).toFixed(1) );
                            $("span[name=span_full_percent_rate]").html( Number(data.data.fullPercent).toFixed(1));
                            $("#query_avg_over").show();
                        }
                    } else {
                        EpgWeb.Plugin.showTips({content: "请求失败" + ( data == null ? "" : ":" + data.msg  ), type: "error"});
                        $("#query_avg_over").hide();
                    }
                })
            }
        },
        doDownloadExcel: function () {
            StatisticOcr2.Util.doFormGetFile(JSON.parse($("#list").jqGrid("getGridParam", "postData").paramsObj),false);
        },
        doDownloadBatch: function () {
           var idArr =  $('#list').jqGrid('getGridParam','selarrrow');
            if(idArr.length > 0){
                StatisticOcr2.Util.doFormGetFile({INL_id: idArr.join(",")},true);
            }
        },
        downloadData: function (elementId) {
            var dataArr = $("#list").jqGrid("getData");
            var findData = null;
            $.each(dataArr, function (i, element) {
                if (elementId == element.id) {
                    findData = element;
                    return false;
                }
            })
            if (findData != null) {
                StatisticOcr2.Util.doFormGetFile({EQL_id: findData.id},true);
            }
        },
        doFormGetFile: function (params,needPic) {
            var formElement = $('<form style="display: none"  method="get"></form>');
            formElement.attr("action", StatisticOcr2.urlConfig.getOcrExcelUrl);
            var paramsObj = JSON.stringify(params);
            var inputElement = $('<input name="paramsObj" >')
            inputElement.val(paramsObj);
            inputElement.appendTo(formElement );
            if(needPic == null||needPic === false){
                needPic = 0;
            }else{
                needPic = 1
            }
            var inputElement2 = $('<input name="needPic" value="'+needPic+'" >')
            inputElement2.appendTo( formElement );
            formElement.appendTo($("body"));
            formElement.submit().remove();
        }
    });

});
